package com.juyu.miliao.common.dataSource.tool.distributeOperate;

import java.util.Collection;
import java.util.Comparator;

import static java.util.stream.Collectors.toList;

public interface DistributeResultHandlers {

	@SuppressWarnings("unchecked")
	public static DistributeResultHandler appendCollection(){
		return stream -> stream.flatMap(obj -> ((Collection<Object>)obj).stream()).collect(toList());
	}

    @SuppressWarnings("unchecked")
	public static DistributeResultHandler appendCollection(Comparator<Object> comparator){
		return stream -> stream.flatMap(obj -> ((Collection<Object>)obj).stream()).sorted(comparator).collect(toList());
	}

    @SuppressWarnings("unchecked")
	public static DistributeResultHandler appendCollection(int page, int pageSize){
		return stream -> stream.flatMap(obj -> ((Collection<Object>)obj).stream())
				.skip((page - 1) * pageSize).limit(pageSize).collect(toList());
	}

    @SuppressWarnings("unchecked")
	public static DistributeResultHandler appendCollection(Comparator<Object> comparator, int page, int pageSize){
		return stream -> stream.flatMap(obj -> ((Collection<Object>)obj).stream())
				.sorted(comparator)
				.skip((page - 1) * pageSize).limit(pageSize).collect(toList());
	}

    public static DistributeResultHandler intSum(){
		return stream -> stream.mapToInt(obj -> Integer.parseInt(String.valueOf(obj))).sum();
	}

    public static DistributeResultHandler longSum(){
		return stream -> stream.mapToLong(obj -> Integer.parseInt(String.valueOf(obj))).sum();
	}

    public static DistributeResultHandler noResult(){
		return stream -> null;
	}

}
